home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ZED3DSRC.ZIP / XFORMS.H < prev    next >
C/C++ Source or Header  |  1995-06-19  |  5KB  |  191 lines

  1. #ifndef __XFORM_H
  2. #define __XFORM_H
  3.  
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7.  
  8. /* if you want to use fixedpoint for your base type, uncomment what
  9.    follows: */
  10.  
  11. /*#define use_fixed*/
  12.  
  13. /* if you want to use float for your base type, uncomment what follows: */
  14.  
  15. #define use_float
  16.  
  17.  
  18. #ifdef use_fixed
  19.  
  20.     #include <math.h>
  21.  
  22.     #define REAL fixedpoint
  23.  
  24.     #define mul fixed_mul
  25.     #define div fixed_div
  26.     #define add(a,b) ((a)+(b))
  27.     #define sub(a,b) ((a)-(b))
  28.  
  29.     #define floattoreal(x) ((x)*65536.0)
  30.     #define realtofloat(x) ((x)/65536.0)
  31.     #define radiantoreal(x) ((x)*(32768.0/M_PI))
  32.     #define realtoradian(x) ((x)*(M_PI/32768.0))
  33.     #define fixedtoreal(x) (x)
  34.     #define realtofixed(x) (x)
  35.  
  36.     /* Normally, you should define "better" functions for better sqrt,
  37.        sin and cos using fixed point. For example, orthonormalize
  38.        should be optimized for close-to-normal vectors already using
  39.        taylor approx. If you need +/- .0002 precision for square root
  40.        of numbers between .9604 and 1.0404, you can use sqrt(x)=(1+x)/2 */
  41.     #define SQRT(x) floattoreal(sqrt(realtofloat(x)))
  42.     #define SIN(x) floattoreal(sin(realtoradian(x)))
  43.     #define COS(x) floattoreal(cos(realtoradian(x)))
  44.  
  45. #elif defined use_float
  46.  
  47.     #include <math.h>
  48.  
  49.     #define REAL float
  50.  
  51.     #define mul(a,b) ((a)*(b))
  52.     #define div(a,b) ((a)/(b))
  53.     #define add(a,b) ((a)+(b))
  54.     #define sub(a,b) ((a)-(b))
  55.  
  56.     #define floattoreal(x) (x)
  57.     #define realtofloat(x) (x)
  58.     #define radiantoreal(x) ((x)*(.5/M_PI))
  59.     #define realtoradian(x) ((x)*2*M_PI)
  60.     #define fixedtoreal(x) ((x)/65536.0)
  61.     #define realtofixed(x) ((x)*65536l)
  62.  
  63.     #define SQRT sqrt
  64.     #define SIN(x) sin(realtoradian(x))
  65.     #define COS(x) cos(realtoradian(x))
  66.  
  67. #else
  68.     #error use_fixed or use_float has to be defined in xforms.h
  69. #endif
  70.  
  71.  
  72.  
  73. typedef long fixedpoint;
  74. /* if you want to use fixedpoint, you have to make sure the functions
  75.    below (add, sub, mul, div) are implemented for your platform */
  76.  
  77. fixedpoint fixed_mul(fixedpoint a, fixedpoint b);
  78. fixedpoint fixed_div(fixedpoint a, fixedpoint b);
  79. fixedpoint fixed_add(fixedpoint a, fixedpoint b);
  80. fixedpoint fixed_sub(fixedpoint a, fixedpoint b);
  81.  
  82.  
  83. typedef REAL vector[3];
  84. typedef vector matrix[3];
  85.  
  86.  
  87. /* For internal reasons, matrices are indexed as matrix[column][row]
  88.    instead of the usual matrix[row][column], e.g. matrix is an array
  89.    of three _column_ vectors (not rows) */
  90.  
  91.  
  92. struct affine_struct
  93.     {
  94.     matrix m;
  95.     vector v;
  96.     };
  97.  
  98.  
  99. typedef struct affine_struct affine;
  100.  
  101. /* The affine type defines an affine transformation, that is, multiply
  102.    by matrix m then translate by vector v. */
  103.  
  104.  
  105. void mat_add(matrix r, matrix a, matrix b);
  106.     /* r=a+b */
  107. void mat_sub(matrix r, matrix a, matrix b);
  108.     /* r=a-b */
  109. void mat_mul(matrix r, matrix a, matrix b);
  110.     /* r=a*b NOTE: r must be different from a and b, though a and b
  111.        can be the same */
  112.  
  113. void mat_mul_vec(vector r, matrix a, vector b);
  114.     /* r=a*b NOTE: b has to be different from r */
  115. void mat_mul_scl(matrix r, matrix a, REAL b);
  116.     /* multiplies matrix a by scalar b and stores in r */
  117. void vec_mul_scl(vector r, vector a, REAL b);
  118.     /* multiplies vector a by a scalar b and stores in r */
  119.  
  120. void vec_add(vector r, vector a, vector b);
  121.     /* r=a+b */
  122. void vec_sub(vector r, vector a, vector b);
  123.     /* r=a-b */
  124.  
  125.  
  126. REAL vec_dot(vector a, vector b);
  127.     /* return a dot b */
  128.  
  129. void vec_crs(vector r, vector a, vector b);
  130.     /* r=a cross b, r HAS to be different from a and b */
  131.  
  132. void vec_normalize(vector a);
  133.     /* normalizes vector a */
  134.  
  135. void mat_orthonormalize(matrix a);
  136.     /* orthonormalizes matrix a (e.g. line vectors will be of unit length
  137.        and perpendicular) */
  138.  
  139. void affine_xform(affine *a, vector i, vector j);
  140.     /* transforms vector i into vector j through affine transform a */
  141.  
  142.  
  143. void initmatrix(matrix m,
  144.     float a00, float a01, float a02,
  145.     float a10, float a11, float a12,
  146.     float a20, float a21, float a22);
  147.     /* stores a00 through a22 in the matrix */
  148.  
  149. void printmatrix(matrix m);
  150.     /* dumps matrix on screen with printf */
  151.  
  152. void initvector(vector v,
  153.     float i, float j, float k);
  154.  
  155. void printvector(vector v);
  156.  
  157. void copymatrix(matrix dest, matrix src);
  158. void copyvector(vector dest, vector src);
  159.  
  160.  
  161. /* angles when parameter is REAL is from 0 to 1 for a complete circle */
  162. void rotatevectors(vector v1, vector v2,
  163.                    vector u1, vector u2,
  164.                    REAL theta);
  165.     /* rotates vector v1 and v2 to u1 and u2, which could be references
  166.        to the same memory location */
  167.  
  168. void rotatebase(matrix m, int axis, REAL theta);
  169.     /* rotates base in matrix m about the given axis by an angle of theta
  170.        Note: you could also make a matrix A of rotation about anything you
  171.        want and then use mat_mul(result,A,m) instead of rotatebase(...).
  172.        This could save you a few muls and stuff.
  173.        axis is either 1, 2 or 3 */
  174.  
  175.  
  176. REAL determinant(matrix m);
  177.     /* returns the determinant of matrix m. used here mainly for finding
  178.        the matrix inverse */
  179. int invert(matrix m, matrix r);
  180.     /* inverses matrix m if possible and puts the result in r. returns
  181.        0 on success, nonzero on error (matrix has no inverse)
  182.        Note that all rotation matrices have an inverse */
  183. int invert_affine(affine *a, affine *r);
  184.     /* inverses affine transformation a into r, which can NOT point to the
  185.        same object. Return value same as invert function */
  186. #ifdef __cplusplus
  187. }
  188. #endif
  189.  
  190. #endif
  191.